package com.asgin;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import java.lang.reflect.Field;

/**
 * Created by Administrator on 2016/8/1.
 */
public class BaseActivity extends Activity {
    protected Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findid(this);



    }

    public void findid(Activity activity){
        try {
            into(activity);
            auto(activity);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public void into(Activity activity){
        Class cls = getClass();
        if (cls.isAnnotationPresent(Asgin.class)) {
            int layout = ((Asgin) cls.getAnnotation(Asgin.class)).value();
            if (layout > 0) {
               activity.setContentView(layout);
            }
        }
    }


   public  void  auto(Activity activity) throws  IllegalAccessError,IllegalAccessException{
       Class clazz=this.getClass();
       Field[] fields=clazz.getDeclaredFields();
       for (Field field:fields){
           if (field.isAnnotationPresent(Asgin.class)){
               Asgin asgin=field.getAnnotation(Asgin.class);
               int id=asgin.value();
               if (id>0){
                   field.setAccessible(true);
                   field.set(activity,activity.findViewById(id));
               }
           }
       }
   }


}
